package com.dgut.common.utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author IT00ZYQ
 * @date 2021/4/15 10:31
 **/
public class DateUtils {

    /**
     * 判断两个日期是否为同一天
     * @param d1 日期1
     * @param d2 日期2
     * @return true/false
     */
    public static boolean sameDate(Date d1, Date d2) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(d1);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(d2);

        if ((c2.getTimeInMillis() - c1.getTimeInMillis())>24*3600*1000) {
            return false;
        }
        return true;
    }

    /**
     * 获取指定月份的第一天
     * @param date 月份
     * @return 第一天
     */
    public static Date firstDateOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getFirstTimeOfDay(date));
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 获取指定月份的最后一天
     * @param date 月份
     * @return 最后一天
     */
    public static Date lastDateOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getLastTimeOfDay(date));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * 获取当天00：00
     */
    public static Date getFirstTimeOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    /**
     * 获取当天23：59
     */
    public static Date getLastTimeOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }


    /**
     * 返回日期列表
     */
    @SuppressWarnings("all")
    public static List<Date> getDateList(Date start, Date end){
        List<Date> list = new ArrayList<>();
        if (start.getDate() == end.getDate()){
            list.add(start);
            return list;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);

        while (calendar.getTime().before(end)){
            list.add(getFirstTimeOfDay(calendar.getTime()));
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        return list;
    }





}
